﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using BussinessObjects;
using BusinessObjects;
using LibraryO2S;
using System.IO;

public partial class controls_admin_MembersEdit : Pages_Base
{
    private DataTable objDT;
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            try
            {
                loadSetting();
                loadData();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally { base.CloseConnection(); }
        }
    }
    private void loadData()
    {
        if (Request["m_id"] == null)
        {
            divMessage.InnerHtml = O2S_Message.Warning("Cảnh Báo", "Bản ghi này không tồn tại hoặc đã bị xóa !");
            return;
        }
        objDT = BO_Members.Search(Convert.ToInt32(Request["m_id"]));
        if (objDT != null && objDT.Rows.Count > 0)
        {
            DataRow row = objDT.Rows[0];
            txtAddress.Text = row["ADDRESS"].ToString();
            txtEamil.Text = row["EMAIL"].ToString();
            txtFax.Text = row["FAX"].ToString();
            txtFullName.Text = row["FULL_NAME"].ToString();
            txtMobile.Text = row["MOBILE"].ToString();
            txtMonney.Text = row["LEFT_MONEY"].ToString();
            cboActive.Checked = Convert.ToBoolean(row["IS_ACTIVE"]);
            cboNewsLetter.Checked = Convert.ToBoolean(row["IS_RECEIVE_NEWSLETTER"]);
            cboPersonal.Checked = Convert.ToBoolean(row["IS_PERSONAL"]);
            cboVip.Checked = Convert.ToBoolean(row["IS_VIP"]);
        }
        else
            divMessage.InnerHtml = O2S_Message.Warning("Hệ Thống", "Bản ghi này không tồn tại hoặc đã bị xóa.");
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            int m_id = Convert.ToInt32(Request["m_id"]);
            objDT = BO_Members.Search(m_id);
            DataRow row = objDT.Rows[0];
            string username = row["USER_NAME"].ToString();
            int region_id = Convert.ToInt32(row["REGION_ID"]);
            string code = row["MEMBER_CODE"].ToString();
            string pass_word = row["PASS_WORD"].ToString();
            DateTime last_login = Convert.ToDateTime(row["LAST_LOGIN"]);
            DateTime last_sms = Convert.ToDateTime(row["LAST_SMS"]);
            DateTime last_recharge = Convert.ToDateTime(row["LAST_RECHARGE"]);
            DateTime created_date = Convert.ToDateTime(row["CREATED_DATE"]);
            string created_by = row["CREATED_BY"].ToString();
            bool is_personal = cboPersonal.Checked;
            bool is_vip = cboVip.Checked;
            bool is_active = cboActive.Checked;
            bool is_receive_newsletter = cboNewsLetter.Checked;
            decimal monney = Convert.ToDecimal(txtMonney.Text.Trim());
            string address = txtAddress.Text.Trim();
            string email = txtEamil.Text.Trim();
            string fax = txtFax.Text.Trim();
            string fullname = txtFullName.Text.Trim();
            string mobile = txtMobile.Text.Trim();
            DataTable admin = (DataTable)Session["Admin"];
            string strResult = BO_Members.Update(m_id, region_id, code, username
                    , pass_word, fullname, address, fax, mobile, email, monney, last_login, last_sms, last_recharge, is_personal, is_vip, is_active, is_receive_newsletter, created_date,
                    created_by, DateTime.Now, admin.Rows[0]["USER_NAME"].ToString(), 1);
            if (strResult != "0")
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Cập nhật thành công!");
            else
                divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Có lỗi trong quá trình xử lý!");
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { base.CloseConnection(); }
    }
    private void loadSetting()
    {
        DataTable objDT = BO_Settings.SelectByGroupId(3, 1);
        foreach (DataRow dr in objDT.Rows)
        {
            string value = dr["SETTING_VALUE"].ToString();
            switch (dr["SETTING_KEY"].ToString())
            {
                //case "NOTE_CATE":
                //    NOTE_CATE.InnerText = value;
                //    break;
                //case "NOTE_DISPLAY":
                //    NOTE_DISPLAY.InnerText = value;
                //    break;
                //case "NOTE_MODULE":
                //    NOTE_MODULE.InnerText = value;
                //    break;
                //case "NOTE_SORT":
                //    NOTE_SORT.InnerText = value;
                //    break;
                //case "NOTE_TITLE":
                //    NOTE_TITLE.InnerText = value;
                //    break;
                //case "NOTE_DESCRIPT":
                //    NOTE_DESCRIPT.InnerText = value;
                //    break;
                //case "NOTE_URL":
                //    NOTE_URL.InnerText = value;
                //    break;
            }
        }
    }
}
